{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# file_operator tools\n",
    "## Tool introduction\n",
    "This is a comprehensive set of file operation tools, you can directly use the related classes or through the agent to achieve the specified root directory of the file operation.\n",
    "File operations include:\n",
    "1. WriteFileTool: Perform a write operation on a specified file in a directory\n",
    "2. AppendFileTool: Perform an append operation on a specified file in a directory\n",
    "3. ReadFileTool: Perform an read operation on a specified file in a directory\n",
    "4. DeleteFileTool: Perform an delete operation on a specified file in a directory\n",
    "5. ListDirectoryTool: Read all files and directories in the directory\n",
    "6. CopyFileTool: Copy the specified files from this directory to the new directory\n",
    "7. MoveFileTool: Move the specified files from this directory to the new directory\n",
    "## Use of tools\n",
    "1. Through a class call\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#（1）Invoke the relevant class directly\n",
    "from promptulate.tools.file import (\n",
    "    WriteFileTool,\n",
    "    CopyFileTool,\n",
    "    ReadFileTool,\n",
    "    ListDirectoryTool,\n",
    "\n",
    ")\n",
    "\n",
    "def main():\n",
    "    # Define the root_dir directory\n",
    "    root_dir = \"/Users/longsihua/Desktop\"\n",
    "    root_dir2 = \"/Users/longsihua/Downloads\"\n",
    "    # Instantiate related classes\n",
    "    write_file_tool = WriteFileTool(root_dir=root_dir)\n",
    "    copy_file_tool = CopyFileTool(root_dir=root_dir)\n",
    "    read_file_tool = ReadFileTool(root_dir=root_dir)\n",
    "    list_directory_tool = ListDirectoryTool(root_dir=root_dir)\n",
    "    \n",
    "    # Call the run method\n",
    "    directort_list = list_directory_tool.run()\n",
    "    print(directort_list,\"\\n\")\n",
    "    write_result = write_file_tool.run(\"hello.txt\", \"Hello, world!\")\n",
    "    print(write_result, \"\\n\")\n",
    "    read_result = read_file_tool.run(\"hello.txt\")\n",
    "    print(read_result, \"\\n\")\n",
    "    copy_result = copy_file_tool.run(\"hello.txt\", root_dir2)\n",
    "    list_directory_tool = ListDirectoryTool(root_dir=root_dir2)\n",
    "    directort_list = list_directory_tool.run()\n",
    "    print(directort_list)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```result\n",
    ".DS_Store\n",
    "Visual Studio Code.app\n",
    "//\n",
    "Write file successfully \n",
    "//\n",
    "Hello, world! \n",
    "//\n",
    ".DS_Store\n",
    "hello.txt\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#（2）Invoke the FileToolKit class\n",
    "from promptulate.tools.file import FileToolKit,ListDirectoryTool\n",
    "\n",
    "def main():\n",
    "    # Define the root_dir directory\n",
    "    root_dir = \"/Users/longsihua/Desktop\"\n",
    "    root_dir2 = \"/Users/longsihua/Downloads\"\n",
    "    # Instantiate related classes\n",
    "    file_tool_kit = FileToolKit(root_dir=root_dir,modes=[\"write\",\"copy\",\"read\",\"list\"])\n",
    "    \n",
    "    Wtool,Ctool,Rtool,Ltool = file_tool_kit.get_tools()\n",
    "\n",
    "    # Call the run method\n",
    "    directort_list = Ltool.run()\n",
    "    print(directort_list,\"\\n\")\n",
    "    write_result = Wtool.run(\"hello.txt\", \"Hello, world!\")\n",
    "    print(write_result, \"\\n\")\n",
    "    read_result = Rtool.run(\"hello.txt\")\n",
    "    print(read_result, \"\\n\")\n",
    "    Ctool.run(\"hello.txt\", root_dir2)\n",
    "    Ltool = ListDirectoryTool(root_dir2)\n",
    "    directort_list = Ltool.run()\n",
    "    print(directort_list)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```result\n",
    ".DS_Store\n",
    "Visual Studio Code.app\n",
    "//\n",
    "Write file successfully \n",
    "//\n",
    "Hello, world! \n",
    "//\n",
    ".DS_Store\n",
    "hello.txt\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. through the agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from promptulate.agents import ToolAgent\n",
    "from promptulate.tools.file import FileToolKit\n",
    "\n",
    "def main():\n",
    "    root_dir = \"/Users/longsihua/Desktop\"\n",
    "    toolkits = FileToolKit(root_dir=root_dir)\n",
    "    tools = toolkits.get_tools()\n",
    "    agent = ToolAgent(tools=tools)\n",
    "    agent.run(\"Please check this directory for me\")\n",
    "    agent.run(\"Please write a bubble_sort in 'sort.py'\")\n",
    "    agent.run(\"Please help me read sort.py\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```result\n",
    "Agent Start...\n",
    "[User] Please check this directory for me\n",
    "[Thought] I can use the 'list-directory' tool to check the directory.\n",
    "[Action] list-directory args: {}\n",
    "[Observation] Visual Studio Code.app\n",
    "c++\n",
    ".DS_Store\n",
    "滕王阁小程序功能介绍.pdf\n",
    ".localized\n",
    "认识实习系统原型\n",
    "Desktop\n",
    "books\n",
    "破解版软件包\n",
    "eatrice\n",
    "pne\n",
    "hello.txt\n",
    "git_programs\n",
    "[Agent Result] The directory contains the following files: Visual Studio Code.app, c++, .DS_Store, 滕王阁小程序功能介绍.pdf, .localized, 认识实习系统原型, Desktop, books, 破解版软件包, eatrice, pne, hello.txt, git_programs.\n",
    "Agent End.\n",
    "\n",
    "Agent Start...\n",
    "[User] Please write a bubble_sort in 'sort.py'\n",
    "[Thought] I need to create a file called 'sort.py' and write a bubble_sort function in it.\n",
    "[Action] write-file args: {'file_name': 'sort.py', 'text': 'def bubble_sort(arr):\\n    n = len(arr)\\n    for i in range(n):\\n        for j in range(0, n-i-1):\\n            if arr[j] > arr[j+1]:\\n                arr[j], arr[j+1] = arr[j+1], arr[j]\\n    return arr'}\n",
    "[Observation] Write file successfully\n",
    "\n",
    "Agent Start...\n",
    "[User] Please help me read sort.py\n",
    "[Thought] I can use the 'read-file' tool to read the 'sort.py' file.\n",
    "[Action] read-file args: {'file_name': 'sort.py'}\n",
    "[Observation] def bubble_sort(arr):\n",
    "    n = len(arr)\n",
    "    for i in range(n):\n",
    "        for j in range(0, n-i-1):\n",
    "            if arr[j] > arr[j+1]:\n",
    "                arr[j], arr[j+1] = arr[j+1], arr[j]\n",
    "    return arr\n",
    "[Agent Result] The content of the 'sort.py' file is a Python function called 'bubble_sort' that implements the bubble sort algorithm.\n",
    "Agent End.\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## attentions\n",
    "There are two parameters in FileToolKit:root_dir and modes.Neither of these parameters is required.\n",
    "\n",
    "If root_dir is empty or not filled，root_dir is the current directory by default.\n",
    "\n",
    "If the modes list is not filled, all is selected by default.\n",
    "\n",
    "The correlation mapping of mode is as follows：\n",
    "\n",
    "\"write\": WriteFileTool,\n",
    "\n",
    "\"append\": AppendFileTool,\n",
    "\n",
    "\"read\": ReadFileTool,\n",
    "\n",
    "\"delete\": DeleteFileTool,\n",
    "\n",
    "\"list\": ListDirectoryTool,\n",
    "\n",
    "\"copy\": CopyFileTool,\n",
    "\n",
    "\"move\": MoveFileTool."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pne",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
